using Godot;
using System;
using System.Data;
using System.Security.Cryptography.X509Certificates;

public partial class Npc : AnimatedSprite2D
{
    [Export] Area2D Area;
    [Export] Label Label;
  
    [Export] ShaderMaterial EnterAreaMaterial;
    private bool playerInArea = false;
    private Material originalMaterial;
    private Node dialogic;
    bool ss = true;
    private bool startedTimeline111 = false;
    private bool dialogStarted = false;
    private bool firstPartCompleted = false;

    
    public override void _Ready()
    {
       
        Label.Visible = false;
        originalMaterial = Material;
        dialogic = GetNode<Node>("/root/Dialogic");
        if (dialogic != null)
        {
            dialogic.Connect("timeline_ended", new Callable(this, nameof(OnDialogicTimelineEnded)));
            dialogic.Connect("signal_event", new Callable(this, nameof(OnDialogicSignal)));
        }
        
        // 进入游戏场景后立即开始对话
        StartDialogFromSignal();
      
        Area.BodyEntered += (Node2D body) =>
        {
            ss = true;
            GD.Print("Player entered Area");
            Label.Visible = true;
            playerInArea = true;
            if (EnterAreaMaterial != null)
            {
                Material = EnterAreaMaterial;
            }
        };
        Area.BodyExited += (Node2D body) =>
        {
            ss = false;
            GD.Print("Player exited Area");
            Label.Visible = false;
            playerInArea = false;
            Material = originalMaterial;
        };
    }
    public override void _Process(double delta)
    {
        if (ss) {
            if (playerInArea && Input.IsActionJustPressed("talk") && !dialogStarted && firstPartCompleted)
            {
                ss = false;
                startedTimeline111 = true;
                dialogStarted = true;
                // 播放第二部分对话
                if (dialogic != null)
                {
                    dialogic.Call("start", "game_continue");
                }
                else
                {
                    GD.PrintErr("Dialogic node not found");
                }
            }
        } }

    private void StartDialogFromSignal()
    {
        
       
        // 确保 dialogic 节点存在
        if (dialogic == null)
        {
            dialogic = GetNodeOrNull<Node>("/root/Dialogic");
        }
        
        if (dialogic != null)
        {
            dialogic.Call("start", "game_start");
            dialogStarted = true;
            startedTimeline111 = true;
        }
        else
        {
            GD.PrintErr("Dialogic node not found at /root/Dialogic");
        }
    }

    private void OnDialogicSignal(string argument)
    {
        if (argument == "close")
        {
            // 当接收到 close 信号时停止对话，等待玩家交互
            if (dialogic != null)
            {
                dialogic.Call("end_timeline");
            }
            dialogStarted = false;
            startedTimeline111 = false;
            firstPartCompleted = true; // 标记第一部分已完成
            GD.Print("First part completed, waiting for player interaction");
        }
        else if (argument == "changestwo")
        {
            // 当接收到 changestwo 信号时结束对话
            if (dialogic != null)
            {
                dialogic.Call("end_timeline");
            }
            dialogStarted = false;
            startedTimeline111 = false;
        }
    }

    private void OnDialogicTimelineEnded()
    {
        if (!startedTimeline111) return;
        startedTimeline111 = false;
        dialogStarted = false;
        
        // 只有在第一部分完成后，对话结束时才跳转场景
        if (firstPartCompleted)
        {
            GD.Print("All dialog completed, changing scene");
            GetTree().ChangeSceneToFile("res://assent/tscn/change_2.tscn");
        }
    }
}
